//  --------------------------------
//  Copyright (c) AW Systems, Inc. All rights reserved.
//  This source code is made available under the terms of the Microsoft Public License (Ms-PL)
//  http://www.codeplex.com/SLAM/license
//  ---------------------------------

using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace AWS.SLAM.Hierarchy
{
    /// <summary>
    /// A SharePoint field for storing hierarchy information for a List Item for usage
    /// in conjunction with the Hierarchical ContentType or List SLAM Type Profiles.
    /// A HierarchyPositionField's value consists of a delimited string of a Parent List Item
    /// GUID Id, a Sibling List Item GUID Id, and a flag indicating whether the item is to be
    /// added or moved.  For all of its parts, the values indicate what state the item should be
    /// in the hierarchy managed by SLAM on saving.  The value does not necessarily reflect the
    /// current position, described by a parent and left sibiling, of an item in the hierarchy.
    /// </summary>
    public class HierarchyPositionField : SPFieldMultiLineText
    {
        bool _isInitialized = false;
        private static Dictionary<int, string> _updatedItemDisplayNameProperty = new Dictionary<int, string>();
        private string _itemDisplayName;

        public int ContextId
        {
            get
            {
                int contextId = 0;
                if (SPContext.Current == null)
                {
                    contextId = 0;
                }
                else
                {
                    contextId = SPContext.Current.GetHashCode();
                }
                return contextId;
            }
        }

        public bool IsInitialized
        {
            get
            {
                return _isInitialized;
            }
            set
            {
                _isInitialized = value;
            }
        }

        public void UpdateItemDisplayNameProperty(string value)
        {
            if (ContextId > 0)
            {
                _updatedItemDisplayNameProperty[ContextId] = value;
            }
        }

        /// <summary>
        /// The name of the Field in the List or Content Type containing this field to be used
        /// as the representative field for an item.  Usually this value is "Title".
        /// </summary>
        public string ItemDisplayName
        {
            get
            {
                this.Init();

                if (ContextId > 0 && _updatedItemDisplayNameProperty.ContainsKey(ContextId))
                    return _updatedItemDisplayNameProperty[ContextId];
                else
                    return _itemDisplayName;
            }
            set
            {
                IsInitialized = true;
                _itemDisplayName = value;
            }
        }

        public HierarchyPositionField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        {  
        }

        public HierarchyPositionField(SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
        {
        }

        public override void OnAdded(SPAddFieldOptions op)
        {
            base.OnAdded(op);
            Update();
        }

        public override BaseFieldControl FieldRenderingControl
        {
            get
            {
                HierarchyPositionFieldControl fieldControl = new HierarchyPositionFieldControl();
                fieldControl.FieldName = InternalName;
                fieldControl.ItemDisplayName = ItemDisplayName;
                return fieldControl;
            }
        }

        public override void Update()
        {
            this.SetCustomProperty("ItemDisplayName", ItemDisplayName);

            base.Update();

            if (ContextId > 0)
            {
                if (_updatedItemDisplayNameProperty.ContainsKey(ContextId))
                    _updatedItemDisplayNameProperty.Remove(ContextId);
            }
        }

        protected virtual void OnInit() { }

        protected void Init()
        {
            if (!IsInitialized)
            {
                if (GetCustomProperty("ItemDisplayName") != null)
                    ItemDisplayName = GetCustomProperty("ItemDisplayName").ToString();

                OnInit();

                IsInitialized = true;
            }
        }
    }
}
